<template>
	<el-card>
		<!-- 搜索 -->
		<el-form :model="searchData" inline ref="searchFormRef" v-resize="searchResize" label-width="90px">
			<el-form-item label="幼儿园名称" prop="kgName">
				<el-input v-model="searchData.kgName" placeholder="请输入幼儿园" clearable></el-input>
			</el-form-item>
			<el-form-item label="班级" prop="className">
				<el-input v-model="searchData.className" placeholder="请输入班级" clearable></el-input>
			</el-form-item>
			<el-form-item label="学生姓名" prop="studentName">
				<el-input v-model="searchData.studentName" placeholder="请输入学生姓名" clearable></el-input>
			</el-form-item>
			
			<el-form-item>
				<el-button icon="el-icon-search" type="primary" @click="searchClick">搜索</el-button>
				<el-button icon="el-icon-refresh" @click="resetClick">重置</el-button>
				<el-button icon="el-icon-plus" type="success" v-has="'sys:student:add'" @click="addShowClick">新增</el-button>
	<!-- 下载模板 -->
				<el-button icon="el-icon-download" type="success"  @click="exportClick">下载模板</el-button> 
	<!-- 导入数据 -->
	<el-upload
				class="upload-demo"
				name="file"
				:headers="headers"
				action="/server/api/sys/student/import"
				:on-success = "uploadContent"
				:on-remove="removeContent"
				>
			<el-button  icon="el-icon-plus" type="primary">导入Excel</el-button>
		</el-upload>


			</el-form-item>
		</el-form>
		<!-- 表格渲染 -->
		<YwTable ref="tableRef" :searchData="searchData" :tableProps="tableProps" api="sysStudentFindAllByPage">
			<el-table-column slot="table_oper" label="操作" fixed="right" width="170px">
				<template v-slot="scope">
					
					<el-button type="text" icon="el-icon-edit" @click="editShowClick(scope.row)"
						v-has="'sys:student:edit'">修改</el-button>
					<el-button type="text" icon="el-icon-delete" @click="deleteClick(scope.row)"
						v-has="'sys:student:delByIds'">删除</el-button>
					
				</template>
			</el-table-column>
		</YwTable>

		<!-- 添加/修改 -->
		<YwDialog @confirm="confirmClick" width="700px" ref="ywDialogRef" title="修改/添加信息">
			<template slot="form">
				<el-form :model="curData" inline ref="curFormRef" label-width="95px" :show-message="false">
				
					<el-form-item label="学生姓名" prop="studentName" required>
						<el-input v-model="curData.studentName" placeholder="请输入学生姓名"></el-input>
					</el-form-item>
			
					<el-form-item label="性别" prop="studentSex" required>
						<el-radio-group v-model="curData.studentSex">
							<el-radio v-for="dict in dict.type.sex" :key="dict.value" :label="dict.value">
								{{dict.label}}
							</el-radio>
						</el-radio-group>
					</el-form-item>

					<el-form-item label="出生日期" prop="studentBirthday" required>
						<el-date-picker
						v-model="curData.studentBirthday"
						style="width:193px"
						value-format="yyyy-MM-dd"
						type="date" 
						>
						></el-date-picker>
					</el-form-item>
					
					<el-form-item label="班级名称" required>
						<el-cascader :options="options" 
						:show-all-levels="false" 
						v-model="value1"
						:props="params" 
						@change="handleChange" clearable filterable></el-cascader>
					</el-form-item>
					
			
					<el-form-item label="学生头像" prop="studentAvatar">
						<YwUpload rootPath="sys/student" v-model="curData.studentAvatar"></YwUpload>
					</el-form-item>
					
					
					
				</el-form>
			</template>
		</YwDialog>

	</el-card>
</template>

<script>
	export default {
		dicts: ["sex"],
		data() {
			return {
				//搜索条件
				searchData: {
					studentName: null,
				
				},
				//添加信息数据
				//修改信息数据
				//表单的值
				curData: {
					studentName: null,
					studentSex: null,
					studentBirthday: null,
					studentAvatar:null,
					kgId: null,
					classId:null,
					
				},
				//表格数据
				tableProps: [{
						label: "学生姓名",
						prop: "studentName"
					},
					{
						label: "性别",
						prop: "studentSex",
						type: "tagDict",
						result: () => {
							return this.dict.type.sex
						}
					},
				
					{
						label: "出生日期",
						prop: "studentBirthday"
					},
					{
						label: "学生头像",
						prop: "studentAvatar",
						type: "avatar"
					},
					{
						label: "幼儿园名称",
						prop: "kgName"
					},
					{
						label: "班级名称",
						prop: "className"
					},
					
				],
				kgList:[],
				classList:[],

				value1:[],
				options: [],
				params: {
					label: 'lable',  
					value: 'value',//指定选项的值为选项对象的某个属性值
					children: 'children' //将后端返回的子选项字段children的值 赋值给默认的children
				} ,
				headers:{}

			};
		},
		mounted() {
			this.headers = this.$http.uploadHeaders();
			this.$nextTick(() => {
				this.$refs.tableRef.shuaxin(this.searchData)
			
			})
			

		},
		methods: {
			//导出模版
			exportClick(){
				window.location.href = "/server/api/sys/student/excel/template"
			},
			

			//获取多级联动的班级id
			handleChange(value) {
				//获取班级id
				let classId;
				classId = value[1];
				console.log(classId);
				this.curData.classId=classId;

			},
			//搜索重置
			searchResize(data) {
				this.$refs.tableRef.refreshSize(data)
			},

			//三级联动数据
			getKgClass(){
				this.$api.sysKgClassList().then(data => {

					this.options = data; 
				});
			},
			//添加/修改表单的确认按钮
			confirmClick() {
				let data = JSON.parse(JSON.stringify(this.curData))
				this.$refs.curFormRef.validate(async (valid) => {
					if (valid) {
						if (data.id) {
							//alert(data.id)
							//修改操作
							await this.$api.sysStudentEdit(data);
						} else {
							//添加操作
							await this.$api.sysStudentAdd(data);
						}
						this.$refs.ywDialogRef.close()
						this.searchClick()
					}
				});
			},
			//删除事件
			deleteClick(row) {
				this.$common.msgBoxDel(() => {
					this.$api.sysStudentDelByIds(row.id).then(res => {
						this.searchClick();
					});
				})
			},

			//新增按钮
			addShowClick() {
				this.curData = {
					studentName: null,
					studentSex: null,
					studentBirthday: null,
					studentAvatar: null,
					kgId: null,
					kgList:[],
					clazzList:[],
				}
				this.getKgClass();
				this.$refs.ywDialogRef.open()
			},
			//修改按钮
			editShowClick(row) {
				//给表单赋值
				this.getKgClass();
				this.$api.sysStudentFindById(row.id).then((result) => {
					this.curData = result;//实现表单赋值
					this.value1 = result.kgClass;
				})	
				this.$refs.ywDialogRef.open()
			},

			//搜索按钮
			searchClick() {
				this.$refs.tableRef.shuaxin(this.searchData)
			},
			//重置按钮
			resetClick() {
				this.$refs.searchFormRef.resetFields();
				this.searchClick()
			},
		},
	};
</script>

<style lang="scss" scoped>
.upload-demo{
	display: inline-block;
	margin-left: 10px;
}
</style>
